package com.irdstudio.efp.console.service.impl;

import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.efp.console.service.dao.SAreaDao;
import com.irdstudio.efp.console.service.domain.SArea;
import com.irdstudio.efp.console.service.facade.SAreaService;
import com.irdstudio.efp.console.service.vo.SAreaVO;
import com.irdstudio.basic.framework.core.base.FrameworkService;
/**
 * <p>ServiceImpl: 省市区街道信息表				<p>
 * @author fuzm
 * @date 2018-09-07
 */
@Service("sAreaService")
public class SAreaServiceImpl implements SAreaService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(SAreaServiceImpl.class);

	@Autowired
	private SAreaDao sAreaDao;
	
	/**
	 * 新增操作
	 */
	public int insertSArea(SAreaVO inSAreaVo) {
		logger.debug("当前新增数据为:"+ inSAreaVo.toString());
		int num = 0;
		try {
			SArea sArea = new SArea();
			beanCopy(inSAreaVo, sArea);
			num = sAreaDao.insertSArea(sArea);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(SAreaVO inSAreaVo) {
		logger.debug("当前删除数据条件为:"+ inSAreaVo);
		int num = 0;
		try {
			SArea sArea = new SArea();
			beanCopy(inSAreaVo, sArea);
			num = sAreaDao.deleteByPk(sArea);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inSAreaVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(SAreaVO inSAreaVo) {
		logger.debug("当前修改数据为:"+ inSAreaVo.toString());
		int num = 0;
		try {
			SArea sArea = new SArea();
			beanCopy(inSAreaVo, sArea);
			num = sAreaDao.updateByPk(sArea);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inSAreaVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public SAreaVO queryByPk(SAreaVO inSAreaVo) {
		
		logger.debug("当前查询参数信息为:"+ inSAreaVo);
		try {
			SArea querySArea = new SArea();
			beanCopy(inSAreaVo, querySArea);
			SArea queryRslSArea = sAreaDao.queryByPk(querySArea);
			if (Objects.nonNull(queryRslSArea)) {
				SAreaVO outSAreaVo = beanCopy(queryRslSArea, new SAreaVO());
				logger.debug("当前查询结果为:"+ outSAreaVo.toString());
				return outSAreaVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public boolean isAgriFalg(List<String> areas) {
		boolean flag = false;
		if (null != areas && areas.size() > 0 ) {
			for (String areaCode : areas){
				if (sAreaDao.queryIsAgriCount(areaCode) > 0 ){
					flag = true;
					break;
				}
			}
		}
		return flag;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SAreaVO> queryAllOwner(SAreaVO sAreaVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<SAreaVO> list = null;
		try {
			List<SArea> sAreas = sAreaDao.queryAllOwnerByPage(sAreaVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ sAreas.size());
			pageSet(sAreas, sAreaVo);
			list = (List<SAreaVO>) beansCopy(sAreas, SAreaVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SAreaVO> queryAllCurrOrg(SAreaVO sAreaVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<SArea> sAreas = sAreaDao.queryAllCurrOrgByPage(sAreaVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+sAreas.size());
		List<SAreaVO> list = null;
		try {
			pageSet(sAreas, sAreaVo);
			list = (List<SAreaVO>) beansCopy(sAreas, SAreaVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SAreaVO> queryAllCurrDownOrg(SAreaVO sAreaVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<SArea> sAreas = sAreaDao.queryAllCurrDownOrgByPage(sAreaVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ sAreas.size());
		List<SAreaVO> list = null;
		try {
			pageSet(sAreas, sAreaVo);
			list = (List<SAreaVO>) beansCopy(sAreas, SAreaVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}
}
